package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.Unknown;

import java.util.*;
import java.util.function.BiFunction;
import java.util.function.IntUnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;

/**
 * https://leetcode.com/problems/implement-magic-dictionary/
 */
public class LC676  implements Unknown {
    class MagicDictionary {
        Map<String, Set<String>> dict = new HashMap<>();

        public MagicDictionary() {
        }

        public void buildDict(String[] dictionary) {
            for (String word : dictionary) {
                for (String wildcard : toWildcard(word)) {
                    dict.compute(wildcard, (k, originWords) -> {
                        if (originWords == null) originWords = new HashSet<>();
                        originWords.add(word);
                        return originWords;
                    });
                }
            }
        }

        List<String> toWildcard(String word) {
            return IntStream.range(0, word.length())
                    .mapToObj(i -> word.substring(0, i) + "*"
                            + word.substring(i + 1, word.length()))
                    .collect(Collectors.toList());
        }

        public boolean search(String searchWord) {
            for (String searchWildcard : toWildcard(searchWord)) {
                Set<String> originWords = dict.get(searchWildcard);
                if (originWords != null) {
                    for (String originWord : originWords) {
                        if (!originWord.equals(searchWord)) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println("123".substring(3, 3));
        LC676 x = new LC676();
        MagicDictionary magic = x.new MagicDictionary();
        System.out.println(magic.toWildcard("a"));
        System.out.println(magic.toWildcard("ab"));
        magic.buildDict(new String[]{"ab", "a", "bcd"});
        System.out.println(magic.search("a"));
        System.out.println(magic.search("ac"));
        System.out.println(magic.search("acd"));

    }
}
